﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_NhaXuatBan : DevExpress.XtraEditors.XtraForm
    {
        public frm_NhaXuatBan()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaNhaXuatBan;

        public int MaNhaXuatBan
        {
            get { return _MaNhaXuatBan; }
            set { _MaNhaXuatBan = value; }
        }
        private string _TenNhaXuatBan;

        public string TenNhaXuatBan
        {
            get { return _TenNhaXuatBan; }
            set { _TenNhaXuatBan = value; }
        }
        private string _DiaChi;

        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        private string _SoDienThoai;

        public string SoDienThoai
        {
            get { return _SoDienThoai; }
            set { _SoDienThoai = value; }
        }
        private string _Fax;

        public string Fax
        {
            get { return _Fax; }
            set { _Fax = value; }
        }
        private string _Email;

        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        private string _GhiChu;

        public string GhiChu
        {
            get { return _GhiChu; }
            set { _GhiChu = value; }
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        public void EnabledTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                //ctr.Text = string.Empty;
                ctr.Enabled = false;
            }
            foreach (Control i in ctr.Controls)
            {
                EnabledTextFalse(i);
            }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                //ctr.Text = string.Empty;
                ctr.Text =string.Empty;
            }
            foreach (Control i in ctr.Controls)
            {
                Huy_Text(i);
            }
        }
        public void GetData()
        {
            txt_MaNXB.Text = MaNhaXuatBan.ToString();
            txt_TenNXB.Text = TenNhaXuatBan;
            txt_DiaChi.Text = DiaChi;
            txt_SoDT.Text = SoDienThoai;
            txt_Fax.Text = Fax;
            txt_Email.Text = Email;
            txt_GhiChu.Text = GhiChu;
        }
        NhaXuatBan_BLL nxb_bll = new NhaXuatBan_BLL();
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Nhà Xuất Bản";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Nhà Xuất Bản";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Nhà Xuất Bản";
                bbi_Luu.Enabled = false;
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                EnabledTextFalse(this);
                GetData();
            }
        }
        private void frm_NhaXuatBan_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NhaXuatBan_Public nxb = new NhaXuatBan_Public();
            if (Flag == 0)
            {
                //them
                nxb.TenNhaXuatBan = txt_TenNXB.Text;
                nxb.DiaChi = txt_DiaChi.Text;
                nxb.SoDienThoai = txt_SoDT.Text;
                nxb.Fax = txt_Fax.Text;
                nxb.Email = txt_Email.Text;
                nxb.GhiChu = txt_GhiChu.Text;
                nxb_bll.insert_nhaXuatBan(nxb, nxb, nxb, nxb, nxb, nxb);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Nhà xuất bản: '" + txt_TenNXB.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //sua
                nxb.MaNhaXuatBan =int.Parse( txt_MaNXB.Text);
                nxb.TenNhaXuatBan = txt_TenNXB.Text;
                nxb.DiaChi = txt_DiaChi.Text;
                nxb.SoDienThoai = txt_SoDT.Text;
                nxb.Fax = txt_Fax.Text;
                nxb.Email = txt_Email.Text;
                nxb.GhiChu = txt_GhiChu.Text;
                nxb_bll.update_nhaXuatBan(nxb,nxb, nxb, nxb, nxb, nxb, nxb);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Nhà xuất bản: '" + TenNhaXuatBan.ToString() + "' thành: '" +txt_TenNXB.Text+ "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //xem
                nxb.MaNhaXuatBan = int.Parse(txt_MaNXB.Text);
                nxb.TenNhaXuatBan = txt_TenNXB.Text;
                nxb.DiaChi = txt_DiaChi.Text;
                nxb.SoDienThoai = txt_SoDT.Text;
                nxb.Fax = txt_Fax.Text;
                nxb.Email = txt_Email.Text;
                nxb.GhiChu = txt_GhiChu.Text;
                nxb_bll.update_nhaXuatBan(nxb, nxb, nxb, nxb, nxb, nxb, nxb);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Nhà xuất bản: '" + TenNhaXuatBan.ToString() + "' thành: '" + txt_TenNXB.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            MyGetData(1);
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }

        private void btn_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 4;
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            Huy_Text(this);
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            txt_MaNXB.Enabled = false;
            txt_MaNXB.Text = "Mã tự sinh";
            this.TenNhaXuatBan = "Thêm Mới Nhà Xuất Bản";
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            this.Text = "Sửa Nhà Xuất Bản";

        }

        private void txt_MaNXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaNXB_Leave(object sender, EventArgs e)
        {
            if (txt_MaNXB.Text == "")
                txt_MaNXB.Focus();
        }

    }
}